package cn.trkj.server;

/**
 * @author admin
 * @date 2020/2/29 1:09 下午
 */

import cn.trkj.server.filter.SystemFilter;
import cn.trkj.server.servlet.FileDownloadServlet;
import cn.trkj.server.servlet.FileUploadServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.DispatcherType;
import java.util.EnumSet;

public class ServerApplication {
    public static void main(String[] args){
        try{
        Server server = new Server(9080);
        System.out.println("========启动==========");
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        context.addServlet(new ServletHolder(new FileUploadServlet()), "/file/upload");
        context.addServlet(new ServletHolder(new FileDownloadServlet()), "/file/download");
        ServletHandler handler = new ServletHandler();
        FilterHolder fh = handler.addFilterWithMapping(SystemFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST));

        server.start();
        server.join();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}